<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller\Backend;

use App\Constants\ErrorCode;
use App\Constants\SuccessCode;
use App\Exception\BusinessException;
use App\Model\Menu;
use App\Model\Role;
use App\Model\RoleMenu;
use App\Resource\Backend\BaseResource;
use App\Controller\AbstractController;
use App\Resource\BaseCollection;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;

class Roles extends AbstractController
{

    public function index(RequestInterface $request, ResponseInterface $response)
    {
        $where = [];

        if ($request->input('role_name')) {
            $where[] = ['role_name', 'like', '%' . $request->input('role_name') . '%'];
        }

        $list = Role::where($where)->paginate();

        return BaseResource::collection($list)->additional(['code' => 0, 'msg' => '成功']);
    }

    public function roleAdd(RequestInterface $request)
    {
        Role::create([
            'role_name' => $request->input('role_name'),
            'role_en' => $request->input('role_en'),
        ]);

        return new \App\Resource\BaseResource();
    }

    public function roleEdit(RequestInterface $request)
    {
        Role::find($request->input('id'))->update([
            'role_name' => $request->input('role_name'),
            'role_en' => $request->input('role_en'),
        ]);

        return new \App\Resource\BaseResource();
    }

    public function roleDel(RequestInterface $request)
    {
        if ($request->input('id') == 1) {
            throw new BusinessException(ErrorCode::ADMIN_SUPER_DEL_ERROR);
        }

        Role::find($request->input('id'))->delete();
        return new \App\Resource\BaseResource();
    }

    public function getMenusWithRole(RequestInterface $request)
    {
        $menus = Menu::with('children')->where('pid', 0)->orderBy('level', 'DESC')->get(['id', 'title']);

        $roleMenu = RoleMenu::where('role_id', $request->input('role_id'))->whereNotIn('menu', Menu::where('pid', '=', 0)->get()->pluck('id'))->get()->pluck('menu');

        return new \App\Resource\BaseResource(SuccessCode::SUCCESS, [
            'menus' => $menus,
            'role_menu' => $roleMenu,
        ]);
    }

    public function setMenusWithRole(RequestInterface $request)
    {
        RoleMenu::where('role_id', $request->input('role_id'))->delete();

        $pids = Menu::whereIn('id', $request->input('menu_ids'))->get()->pluck('pid');

        foreach ($pids as $item) {

            RoleMenu::create([
                'role_id' => $request->input('role_id'),
                'menu' => $item,
            ]);
        }

        foreach ($request->input('menu_ids') as $item) {
            RoleMenu::create([
                'role_id' => $request->input('role_id'),
                'menu' => $item,
            ]);
        }
        return new \App\Resource\BaseResource();
    }

    public function selectRoles(RequestInterface $request)
    {
        $roles = Role::get();
        $list = [];
        foreach ($roles as $role) {
            $list[] = [
                'value' => $role->id,
                'label' => $role->role_name,
            ];
        }

        return new \App\Resource\BaseResource(SuccessCode::SUCCESS, $list);
    }

    public function roleMenus(RequestInterface $request)
    {
        $menus = Menu::with('children')->whereIn('id', RoleMenu::where('role_id', $this->session->get('admin_user')->role_id)->orderBy('menu')->pluck('menu'))->where('pid', 0)->orderBy('pid')->orderByDesc('level')->get();

        return new \App\Resource\BaseResource(SuccessCode::SUCCESS, $menus);
    }


    public function menus(RequestInterface $request)
    {
        $where = [];
        if($request->input('title')){

            $where[] = ['title', 'like', '%' . $request->input('title') . '%'];
        }

        $menus = Menu::with('children')->where('pid', 0)->where($where)->orderBy('level', 'DESC')->paginate();

        return new BaseCollection($menus);
    }

    public function menuAdd(RequestInterface $request)
    {
        Menu::create($request->all());

        return new \App\Resource\BaseResource();
    }

    public function menuEdit(RequestInterface $request)
    {
        Menu::find($request->input('id'))->update($request->all());

        return new \App\Resource\BaseResource();
    }

    public function menuDel(RequestInterface $request)
    {
        Menu::find($request->input('id'))->delete();

        return new \App\Resource\BaseResource();
    }

}
